package com.flykoc.lightinggem.dao;

import com.flykoc.lightinggem.dto.LightingGem;
import com.google.common.collect.Maps;
import org.bukkit.inventory.ItemStack;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * 宝石数据缓冲
 *
 * @author Zoyn
 * @since 2017-10-22
 */
public class GemDao {

    private static Map<String, LightingGem> map = Maps.newHashMap();

    public static void putGem(String gemName, LightingGem gem) {
        map.put(gemName, gem);
    }

    public static void removeGem(String gemName) {
        if (map.containsKey(gemName)) {
            map.remove(gemName);
        }
    }

    public static LightingGem getGem(String gemName) {
        return map.getOrDefault(gemName, null);
    }

    public static LightingGem getGem(ItemStack itemStack) {
        if (!itemStack.hasItemMeta()) {
            return null;
        }
        if (!itemStack.getItemMeta().hasDisplayName()) {
            return null;
        }

        Optional<LightingGem> gemOptional = getGems()
                .stream()
                .filter(lightingGem -> lightingGem.getItemStack().getItemMeta().equals(itemStack.getItemMeta()))
                .findFirst();

        return gemOptional.orElse(null);
    }

    public static Set<String> getKeys() {
        return map.keySet();
    }

    public static Collection<LightingGem> getGems() {
        return map.values();
    }

    public static void clearMap() {
        map.clear();
    }
}
